package com.starsoft.frame.core.orm;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@SuppressWarnings("unchecked")
public abstract class BaseDao<T, PK extends Serializable> extends HibernateDao implements IBaseDao<T, PK> {

	public Logger logger = LoggerFactory.getLogger(this.getClass());
	
	protected Class<T> entityClass;

	public BaseDao() {
		try {
			Type genType = getClass().getGenericSuperclass();
			Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
			entityClass = (Class<T>) params[0];
		} catch (Exception e) {
			entityClass = null;
		}
	}

	@Override
	public T get(PK id) {
		return get(entityClass, id);
	}

	public T getByMap(Map<String, Object> params) {
		return getByMap(entityClass, params);
	}
	
	@Override
	public List<T> getAll() {
		return getAll(entityClass);
	}

	@Override
	public List<T> findByMap(Map<String, Object> params) {
		return findByMap(entityClass, params);
	}

	@Override
	public Map<String, Object> pageQuery(PageObject po) {
		return pageQuery(entityClass, po, new HashMap<String, Object>());
	}
	
}
